home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxclfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.0 KB  |  165 lines

  1. /* Copyright (C) 1995, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxclfile.c,v 1.2 2000/09/19 19:00:34 lpd Exp $ */
  20. /* File-based command list implementation */
  21. #include "stdio_.h"
  22. #include "string_.h"
  23. #include "gserror.h"
  24. #include "gserrors.h"
  25. #include "gsmemory.h"
  26. #include "gp.h"
  27. #include "gxclio.h"
  28.  
  29. /* This is an implementation of the command list I/O interface */
  30. /* that uses the file system for storage. */
  31.  
  32. /* ------ Open/close/unlink ------ */
  33.  
  34. int
  35. clist_fopen(char fname[gp_file_name_sizeof], const char *fmode,
  36.         clist_file_ptr * pcf, gs_memory_t * mem, gs_memory_t *data_mem,
  37.         bool ok_to_compress)
  38. {
  39.     if (*fname == 0) {
  40.     if (fmode[0] == 'r')
  41.         return_error(gs_error_invalidfileaccess);
  42.     *pcf =
  43.         (clist_file_ptr) gp_open_scratch_file(gp_scratch_file_name_prefix,
  44.                           fname, fmode);
  45.     } else
  46.     *pcf = gp_fopen(fname, fmode);
  47.     if (*pcf == NULL) {
  48.     eprintf1("Could not open the scratch file %s.\n", fname);
  49.     return_error(gs_error_invalidfileaccess);
  50.     }
  51.     return 0;
  52. }
  53.  
  54. int
  55. clist_fclose(clist_file_ptr cf, const char *fname, bool delete)
  56. {
  57.     return (fclose((FILE *) cf) != 0 ? gs_note_error(gs_error_ioerror) :
  58.         delete ? clist_unlink(fname) :
  59.         0);
  60. }
  61.  
  62. int
  63. clist_unlink(const char *fname)
  64. {
  65.     return (unlink(fname) != 0 ? gs_note_error(gs_error_ioerror) : 0);
  66. }
  67.  
  68. /* ------ Writing ------ */
  69.  
  70. long
  71. clist_space_available(long requested)
  72. {
  73.     return requested;
  74. }
  75.  
  76. int
  77. clist_fwrite_chars(const void *data, uint len, clist_file_ptr cf)
  78. {
  79.     return fwrite(data, 1, len, (FILE *) cf);
  80. }
  81.  
  82. /* ------ Reading ------ */
  83.  
  84. int
  85. clist_fread_chars(void *data, uint len, clist_file_ptr cf)
  86. {
  87.     FILE *f = (FILE *) cf;
  88.     byte *str = data;
  89.  
  90.     /* The typical implementation of fread */
  91.     /* is extremely inefficient for small counts, */
  92.     /* so we just use straight-line code instead. */
  93.     switch (len) {
  94.     default:
  95.         return fread(str, 1, len, f);
  96.     case 8:
  97.         *str++ = (byte) getc(f);
  98.     case 7:
  99.         *str++ = (byte) getc(f);
  100.     case 6:
  101.         *str++ = (byte) getc(f);
  102.     case 5:
  103.         *str++ = (byte) getc(f);
  104.     case 4:
  105.         *str++ = (byte) getc(f);
  106.     case 3:
  107.         *str++ = (byte) getc(f);
  108.     case 2:
  109.         *str++ = (byte) getc(f);
  110.     case 1:
  111.         *str = (byte) getc(f);
  112.     }
  113.     return len;
  114. }
  115.  
  116. /* ------ Position/status ------ */
  117.  
  118. int
  119. clist_set_memory_warning(clist_file_ptr cf, int bytes_left)
  120. {
  121.     return 0;            /* no-op */
  122. }
  123.  
  124. int
  125. clist_ferror_code(clist_file_ptr cf)
  126. {
  127.     return (ferror((FILE *) cf) ? gs_error_ioerror : 0);
  128. }
  129.  
  130. long
  131. clist_ftell(clist_file_ptr cf)
  132. {
  133.     return ftell((FILE *) cf);
  134. }
  135.  
  136. void
  137. clist_rewind(clist_file_ptr cf, bool discard_data, const char *fname)
  138. {
  139.     FILE *f = (FILE *) cf;
  140.  
  141.     if (discard_data) {
  142.     /*
  143.      * The ANSI C stdio specification provides no operation for
  144.      * truncating a file at a given position, or even just for
  145.      * deleting its contents; we have to use a bizarre workaround to
  146.      * get the same effect.
  147.      */
  148.     char fmode[4];
  149.  
  150.     /* Opening with "w" mode deletes the contents when closing. */
  151.     freopen(fname, gp_fmode_wb, f);
  152.     strcpy(fmode, "w+");
  153.     strcat(fmode, gp_fmode_binary_suffix);
  154.     freopen(fname, fmode, f);
  155.     } else {
  156.     rewind(f);
  157.     }
  158. }
  159.  
  160. int
  161. clist_fseek(clist_file_ptr cf, long offset, int mode, const char *ignore_fname)
  162. {
  163.     return fseek((FILE *) cf, offset, mode);
  164. }
  165.